import VueRouter from "vue-router"
//引入路由
import login from '@/view/login/login.vue'
// //引入组件
import HomePage from '@/layout/homePage.vue'

import bookManage from '@/view/bookmanage/bookManage'

import index from '@/view/index'

const router = new VueRouter({
    routes: [
        {
            path: '/',
            component: login,
        },
        {
            path: '/login',
            component: login,
        },
        {
            path: '/homepage',
            component: HomePage,
            children: [
                {
                    path: 'index',
                    component: index
                },
                {
                    path: 'bookManage',
                    component: bookManage
                },
            ]
        } 
    ]
})
//全局前置路由守卫
router.beforeEach((to, from, next) => {
        //首先判断是否为登录页
        if(to.path!=='/'){
            //如果不是登录页，继续判断是否有token
            if(!sessionStorage.token){
                //如果没有token，不能访问后台，跳回登录页
                next('/')
                return
            }
        }
       next() 
})

export default router
//创建并暴露一个路由器